<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;

class Category extends AdminBase
{
    public $icons = ['asterisk','plus','glass','phone','euro','minus','cloud','envelope','pencil','music','search','heart','star','star-empty'
        ,'user','film','th','th-large','th-list','ok','remove','zoom-in','zoom-out','off','signal','cog','trash','home','file','time'
        ,'road','download-alt','download','upload','inbox','play-circle','repeat','refresh','list-alt','lock'];

    public function index()
    {
        //查询出所有数据
        $assign['list'] = category_format( model('Category')->select() );
        $this->assign($assign);
        return $this->fetch();
    }

    public function add()
    {
        if( $this->request->isPost() )
        {
            $validate = new \app\admin\validate\Category\Add;
            $param = $this->request->param();
            if (!$validate->check($param))
            {
                return $this->error($validate->getError());
            }
            else
            {
                $id = model('Category')->insert($param);
                if( $id )
                {
                    return $this->success('操作成功', url('/admin/category/'));
                }
                else
                {
                    return $this->error(model('Category')->getError());
                }
            }
        }
        else
        {
            //获取所有一级分类
            $assign['top_categories'] = model('Category')->where("pid", 0)->select()->toArray();
//            $assign['icons'] = ['asterisk','plus','glass','phone','euro','minus','cloud','envelope','pencil','music','search','heart','star','star-empty'
//                                ,'user','film','th','th-large','th-list','ok','remove','zoom-in','zoom-out','off','signal','cog','trash','home','file','time'
//                                ,'road','download-alt','download','upload','inbox','play-circle','repeat','refresh','list-alt','lock'];
            $assign['icons'] = $this->icons;
            $this->assign($assign);
            return $this->fetch();
        }
    }

    public function edit()
    {
        $id = $this->request->param('id');
        if( $this->request->isPost() )
        {
            $validate = new \app\admin\validate\Category\Edit;
            $param = $this->request->param();
            if (!$validate->check($param))
            {
                return $this->error($validate->getError());
            }
            else
            {
                $id = model('Category')->save($param, $id);
                if( $id !== false )
                {
                    return $this->success('操作成功', url('/admin/category/'));
                }
                else
                {
                    return $this->error(model('Category')->getError());
                }
            }
        }
        else
        {
            //获取信息
            $assign['info'] = model('Category')->find($id);
            //获取所有一级分类
            $assign['top_categories'] = model('Category')->where([['pid','=',0],['id','<>',$id]])->select()->toArray();
            $assign['icons'] = $this->icons;
            $this->assign($assign);
            return $this->fetch();
        }
    }

    public function del(){
        $id = $this->request->param('id');
        $validate = new \app\admin\validate\Category\Del;
        if (!$validate->check(['id'=>$id]))
        {
            return $this->error($validate->getError());
        }
        else
        {
            $res = model('Category')->where('id', $id)->delete();
            if( $res !== false )
            {
                return $this->success('操作成功', url('/admin/category/'));
            }
            else
            {
                return $this->error(model('Category')->getError());
            }
        }
    }

    public function ajax_get_sec_categories(){
        $top_category_id = $this->request->param('top_category_id');
        if( $top_category_id )
        {
            $res = model('Category')->where('pid', $top_category_id)->select();
        }
        else
        {
            $res = false;
        }
        if( $res !== false )
        {
            return $this->success('操作成功', url('/admin/category/'), $res);
        }
        else
        {
            return $this->error(model('Category')->getError());
        }
    }
}
